{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 购物车项目训练\n",
    "\n",
    "\n",
    "- 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "- 让用户输入商品编号进行商品的购买。\n",
    "- 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "- 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入支付宝余额48000\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号：5\n",
      "你已经将商品watch添加到购物车，你的支付宝余额为38000\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号：1\n",
      "你已经将商品mac pro添加到购物车，你的支付宝余额为22000\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号：6\n",
      "你已经将商品head first python添加到购物车，你的支付宝余额为21872\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号：1\n",
      "你已经将商品mac pro添加到购物车，你的支付宝余额为5872\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号：5\n",
      "你的支付宝余额不足，请选择其他商品\n",
      "0 ('华为p40', 4800)\n",
      "1 ('mac pro', 16000)\n",
      "2 ('milk tea', 18)\n",
      "3 ('crabs', 88)\n",
      "4 ('bike', 800)\n",
      "5 ('watch', 10000)\n",
      "6 ('head first python', 128)\n",
      "7 ('book', 100)\n",
      "请输入商品编号：q\n",
      "('watch', 10000)\n",
      "('mac pro', 16000)\n",
      "('head first python', 128)\n",
      "('mac pro', 16000)\n",
      "以上是你已经购买的商品，你的支付宝余额为5872\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    ('华为p40',4800),\n",
    "    ('mac pro',16000),\n",
    "    ('milk tea', 18),\n",
    "    ('crabs', 88),\n",
    "    ('bike', 800),\n",
    "    ('watch', 10000),\n",
    "    ('head first python',128),\n",
    "    ('book', 100)\n",
    "]\n",
    "shopping_car = []\n",
    "zhifubao = input(\"请输入支付宝余额\")\n",
    "if zhifubao.isdigit():\n",
    "    zhifubao = int(zhifubao)\n",
    "    while True:\n",
    "        for item in product_list:\n",
    "            print(product_list.index(item),item)\n",
    "        choice = input(\"请输入商品编号：\")\n",
    "        if choice.isdigit():\n",
    "            choice = int(choice)\n",
    "            if choice >=0 and choice <=7:\n",
    "                if zhifubao >= product_list[choice][1]:\n",
    "                    zhifubao = zhifubao -product_list[choice][1]\n",
    "                    shopping_car.append(product_list[choice])\n",
    "                    print(\"你已经将商品%s添加到购物车，你的支付宝余额为%s\"%(product_list[choice][0],zhifubao))\n",
    "                else:\n",
    "                    print(\"你的支付宝余额不足，请选择其他商品\")\n",
    "            else:\n",
    "                print(\"所选商品不再列表\")\n",
    "        elif choice == \"q\":\n",
    "            for item in shopping_car:\n",
    "                print(item)\n",
    "            print(\"以上是你已经购买的商品，你的支付宝余额为%s\"% zhifubao)\n",
    "            break\n",
    "        else:\n",
    "            print(\"请输入商品编号：\")\n",
    "    else:\n",
    "        print(\"请输入数字余额\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
